package com.my.jli.Service.impl;

import com.my.jli.Service.StudentService;
import com.my.jli.dao.StudentRepository;
import com.my.jli.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @ClassName StudentServiceImpl
 * @Description
 * @Author MaYu
 * @Date 2022/1/20 19:05
 * @Version 1.0
 **/
@Service
public class StudentServiceImpl implements StudentService {

    private StudentRepository studentRepository;
    @Autowired
    public void setStudentRepository(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Student> getAll() {
        return studentRepository.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<Student> getAll(Integer tid, Pageable pageable) {
        return studentRepository.findByTid(tid, pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public Student getById(Integer id) {
        Optional<Student> studentOptional = studentRepository.findById(id);
        return studentOptional.orElse(null);
    }

    @Override
    @Transactional(readOnly = true)
    public Student getByNum(String num) {
        return studentRepository.findByNum(num);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Student> getByTid(Integer tid) {
        return studentRepository.findByTid(tid);
    }

    @Override
    @Transactional(readOnly = true)
    public Student getByNumAndTid(String num, Integer tid) {
        return studentRepository.findByNumAndTid(num, tid);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<Student> getAll(Example<Student> example, Pageable pageable) {
        return studentRepository.findAll(example, pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Student> login(String sNum, String sPwd) {
        return studentRepository.findByNumAndPwd(sNum, sPwd);
    }

    @Override
    @Transactional(readOnly = true)
    public Set<String> getAllGrade() {
        return studentRepository.findGrade();
    }

    @Override
    @Transactional(readOnly = true)
    public Set<String> getAllClazz() {
        return studentRepository.findClazz();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(Student student) {
        studentRepository.save(student);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteById(Integer id) {
        studentRepository.deleteById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Student saveStudent(Student student) {
        return studentRepository.save(student);
    }
}
